package xxy.com.yangproject.ui.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import xxy.com.yangproject.R;
import xxy.com.yangproject.database.Entity.Cheese;

public class DataBaseAdapter extends PagedListAdapter<Cheese, DataBaseAdapter.DataBaseViewHolder> {


    private static DiffUtil.ItemCallback<Cheese> ITEM_CALLBACK  = new DiffUtil.ItemCallback<Cheese>(){

        @Override
        public boolean areItemsTheSame(@NonNull Cheese oldItem, @NonNull Cheese newItem) {
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull Cheese oldItem, @NonNull Cheese newItem) {
            return oldItem.name.equals(newItem.name);
        }
    };

    public DataBaseAdapter() {
        super(ITEM_CALLBACK);
    }


    @NonNull
    @Override
    public DataBaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_database_cheese_item, parent, false);
        return new DataBaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DataBaseViewHolder holder, int position) {
        holder.bindto(getItem(position));
    }

    public static class DataBaseViewHolder extends RecyclerView.ViewHolder{

        private Cheese cheese;
        private final TextView textView;

        public DataBaseViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_database);
        }

        public void bindto(Cheese cheese){
            this.cheese = cheese;
            textView.setText(cheese.name);
        }
    }
}
